<?php
error_reporting(E_ALL ^E_NOTICE);
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onPayProsessStart', 'jeProcessInterwalletStart' );
$_MAMBOTS->registerFunction( 'onPayProsessFinish', 'jeProcessInterwalletFinish' );
//print_r($_POST);
//print_r($_GET);
//exit;
function jeProcessInterwalletFinish()
{
    $processor = mosGetParam( $_REQUEST, 'processor'); // success or fail
    
    if($processor != '.interwallet') return false;
    $order['id']         = mosGetParam( $_REQUEST, 'order_id');
    $order['pay']        = mosGetParam( $_REQUEST, 'pay');
    $order['gateway']    = $processor;
    $order['gateway_id'] = mosGetParam( $_REQUEST, 'txn_id');
    $order['price']     = mosGetParam( $_REQUEST, 'amount');
    
    return $order;
}

function jeProcessInterwalletStart( $params )
{
    Global $my, $database, $mosConfig_live_site, $Itemid;
    
    if($params['processor'] != '.interwallet') return;
    
    $query = "SELECT id"
    . "\n FROM #__mambots"
    . "\n WHERE element = 'joomlaequipment.payprocessor{$params[processor]}'"
    . "\n AND folder = 'system'"
    ;
    $database->setQuery( $query );
    $id     = $database->loadResult();
    $mambot = new mosMambot( $database );
    $mambot->load( $id );
    $botParams = new mosParameters( $mambot->params );
    
    $param['option']    = mosGetParam( $_REQUEST, 'option');
    $param['task']      = 'pay';
    $param['processor'] = $params['processor'];
    $param['order_id']  = $params['order_id'];
    $param['amount']    = $params['order_amount'];
    $param['Itemid']    = $Itemid;
    
    $return             = urlencode($mosConfig_live_site.'/mambots/system/iw_success.php?data='.implode(",", $param));
    $cancel_return      = urlencode($mosConfig_live_site.'/mambots/system/iw_fail.php?data='.implode(",", $param));
    //unset($param);
    
    $param['module'] = "sendmoney";
    $param['display_mode'] = "Merchant";
    $param['FormPage'] = 1;
    
    $param['amount'] = $params['order_amount'];
    $param['currency_code'] = $botParams->def( 'currency');
    $param['source_email'] = $my->email;
    $param['target_email'] = $botParams->def('target_email');
    $param['transaction_details'] = strval($params['order_title']);
    $param['purchase_category'] = 1;
    $param['lang'] = $botParams->def( 'lang');
    $param['source_success_url'] = $return;
    $param['source_failure_url'] = $cancel_return;
    $param['test_mode'] = $botParams->def('test');
    $param['quantity'] = 1;

    if(!$param['target_email'] || !$param['amount'])
    {
        echo "<P><h1>Error Payment Processing: some of the params passed are empty</h1></P>";
        return;
    }
    $url = 'https://www.interwallet.com/index.php?'.jpArray2Url($param);
    echo str_replace("&", "<BR>", $url);
    header("location: $url");
}

if(!defined('JP_IMPLODE'))
{
    define('JP_IMPLODE', 1);
    function jpArray2Url($param)
    {
        foreach($param AS $k => $v)
        {
            $out[] = "$k=$v";
        }
        return implode('&', $out );
    }
}



?>
